﻿using Iworld.basis.Service;
using Iworld.Game.Model;
using Iworld.Game.Service.Backstage.Export;
using Iworld.Main.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Game.Service.Backstage.Import
{
    [DataContract]
    public class GetProfitStatisticsOfPersonImport : GetPageListCheckAdministratorImportBase
    {
        /// <summary>
        /// 账号或名称关键字
        /// </summary>
        [DataMember]
        public string Keyword { set; get; }


        public PageResult<ProfitStatisticsOfPersonExport> GetProfitStatisticsOfPerson(ModelToDbContext db)
        {

            int pageSize = 10;
            int startRow = pageSize * (this.PageIndex - 1);
            IQueryable<Iworld.Author.Model.Author> temp = db.Authors;
            if (Keyword != null && Keyword.Trim().Length > 0)
            {
                temp = temp.Where(x => x.Username.Contains(Keyword)
                    || x.Account.Contains(Keyword));
            }
            int totalCount = temp.Count();
            var list = temp.OrderByDescending(x => x.CreatedTime).Skip(startRow).Take(pageSize).ToList().ConvertAll(x =>
            {
                double allSpade = 0;
                double allHeart = 0;
                double allClub = 0;
                double allDiamond = 0;
                double allKing = 0;
                double allWin = 0;
                db.Bettings.Where(k => k.Owner.Id == x.Id).ToList().ForEach(t => {
                    allSpade += t.Spade;
                    allHeart += t.Heart;
                    allClub += t.Club;
                    allDiamond += t.Diamond;
                    allKing += t.King;
                    allWin += t.Bonus;
                });
                return new ProfitStatisticsOfPersonExport()
                {
                    AllBettingGolds = (allSpade + allHeart + allClub + allDiamond + allKing),
                    AllBettingGoldsOfClub = allClub,
                    AllBettingGoldsOfDiamond = allDiamond,
                    AllBettingGoldsOfHeart = allHeart,
                    AllBettingGoldsOfKing = allKing,
                    AllBettingGoldsOfSpade = allSpade,
                    AllWinGolds = allWin,
                    Account=x.Account, 
                    Username=x.Username,
                    ProfitGolds = (allSpade + allHeart + allClub + allDiamond + allKing) - allWin
                };
            });
            return new PageResult<ProfitStatisticsOfPersonExport>(this.PageIndex, totalCount, pageSize, list);   
        }
    }
}
